﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace Infrastructure
{
    /// <summary>
    /// 实体帮助类
    /// </summary>
    public class ClassHelper
    {
        /// <summary>
        /// 将父类信息赋值给子类
        /// </summary>
        /// <typeparam name="TParent">父类</typeparam>
        /// <typeparam name="TChild">子类</typeparam>
        /// <param name="parent">父类实体</param>
        /// <returns>子类实体</returns>
        public static TChild CopyToChild<TParent, TChild>(TParent parent) where TChild : TParent, new()
        {
            TChild val = new TChild();
            Type typeFromHandle = typeof(TParent);
            PropertyInfo[] properties = typeFromHandle.GetProperties();
            PropertyInfo[] array = properties;
            foreach (PropertyInfo propertyInfo in array)
            {
                if (propertyInfo.CanRead && propertyInfo.CanWrite)
                {
                    propertyInfo.SetValue(val, propertyInfo.GetValue(parent, null), null);
                }
            }
            return val;
        }
    }
}
